﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.Security;
using System.Web;
using ExamAnalysis.Model;

namespace ExamAnalysis.Core
{
    public static class MemberInfoQuery
    {
        public static SchoolInfo getSchoolofMember()
        {
            ExamAnalysisDataContext db=new ExamAnalysisDataContext();
            Guid memberId = (Guid)Membership.GetUser().ProviderUserKey;
            var teacher=from t in db.TeacherInfo
                        where t.AccountID==memberId.ToString()
                        select t;
            if(teacher.Count()==0)
            {
            var student=from s in db.StudentInfo
                        where s.AccountID==memberId.ToString()
                        select s;
                if (student.Count()==0)
                    throw new UserException("未能找到绑定账户！");
                else 
                {
                    return student.First().ClassInfo.SchoolInfo;

                }
            
            }
            else
            {
            return teacher.First().SchoolInfo;
            }
        }

        public static ClassInfo getClassofMember()
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            Guid memberId = (Guid)Membership.GetUser().ProviderUserKey;
            var teacher = from t in db.TeacherInfo
                          where t.AccountID == memberId.ToString()
                          select t;
            if (teacher.Count() == 0)
            {
                var student = from s in db.StudentInfo
                              where s.AccountID == memberId.ToString()
                              select s;
                if (student.Count() == 0)
                    throw new UserException("未能找到绑定账户！");
                else
                {
                    return student.First().ClassInfo;

                }

            }
            else
            {
                return null;
            }
        }
    }
}
